package com.inhim.pjhim.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.utils.ImageLoaderUtils;
import com.inhim.pjhim.view.CustomRoundAngleImageView;

import java.util.List;

public class ChapterItemGridviewAdapter extends BaseAdapter {
    private List<ReaderTypeList.List> datas;
    private Context context;

    public ChapterItemGridviewAdapter(ReaderTypeList datas, Context context) {
        this.datas = datas.getPage().getList();
        this.context = context;
    }

    @Override
    public int getCount() {
        if(datas.size()>8){
            //最多显示8个item
            return 8;
        }else{
            return datas.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View mView, ViewGroup viewGroup) {
        try {
            PersonViewHolder viewHolder;
            if (mView == null) {
                viewHolder = new PersonViewHolder();
                mView = LayoutInflater.from(context).inflate(R.layout.item_chapter_list, null);
                viewHolder.circleImageView = mView.findViewById(R.id.circleImageView);
                viewHolder.textview = mView.findViewById(R.id.textview);
                viewHolder.constran = mView.findViewById(R.id.constran);
                mView.setTag(viewHolder);
            } else {
                viewHolder = (PersonViewHolder) mView.getTag();
            }
            if(position==7){
                viewHolder.circleImageView.setImageResource(R.mipmap.look_all);
                viewHolder.textview.setText(context.getResources().getString(R.string.look_all));
            }else {
                ImageLoaderUtils.setImage(datas.get(position).getIcon(), viewHolder.circleImageView);
                viewHolder.textview.setText(datas.get(position).getName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mView;
    }

    public class PersonViewHolder  {

        public CustomRoundAngleImageView circleImageView;
        public TextView textview;
        public ConstraintLayout constran;

            /*circleImageView = itemView.findViewById(R.id.circleImageView);
            textview = itemView.findViewById(R.id.textview);
            constran = itemView.findViewById(R.id.constran);
            //动态计算屏幕宽度 一行显示四个item
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) ((context.getResources().getDisplayMetrics().
                    widthPixels - ScreenUtils.dip2px(context, 5)) / 4f),

                    ViewGroup.LayoutParams.MATCH_PARENT);
            constran.setLayoutParams(params);*/

    }
}
